{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Loader(object): # basic loader class \n",
    "    def __init__(self, path, count):\n",
    "        self.path = path\n",
    "        self.count = count\n",
    "    def get_file_content(self):\n",
    "        print(self.path)\n",
    "        f = open(self.path, 'rb')\n",
    "        content = f.read()\n",
    "        f.close()\n",
    "        return content  #count number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ImageLoader(Loader):\n",
    "    # return 28*28*n list\n",
    "    def get_picture(self, content, index):\n",
    "        start = index * 28 * 28 + 16 # size 28*28 + 16head\n",
    "        picture = []\n",
    "        for i in range(28):\n",
    "            picture.append([])\n",
    "            for j in range(28):\n",
    "                byte1 = content[start + i * 28 + j]\n",
    "                picture[i].append(byte1)\n",
    "        return picture   \n",
    "\n",
    "    # return array size 784\n",
    "    def get_one_sample(self, picture):\n",
    "        sample = []\n",
    "        for i in range(28):\n",
    "            for j in range(28):\n",
    "                sample.append(picture[i][j])\n",
    "        return sample  \n",
    "\n",
    "    # load and exicute\n",
    "    def load(self,onerow=False):\n",
    "        content = self.get_file_content()\n",
    "        data_set = []\n",
    "        for index in range(self.count):  \n",
    "            onepic =self.get_picture(content, index)  \n",
    "            if onerow: onepic = self.get_one_sample(onepic)  \n",
    "            data_set.append(onepic)\n",
    "        return data_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# label\n",
    "class LabelLoader(Loader):\n",
    "    # load image's label\n",
    "    def load(self):\n",
    "        content = self.get_file_content()   \n",
    "        labels = []\n",
    "        for index in range(self.count): \n",
    "            onelabel = content[index + 8]  \n",
    "            onelabelvec = self.norm(onelabel) \n",
    "            labels.append(onelabelvec)\n",
    "        return labels\n",
    "    \n",
    "    #label as vector\n",
    "    def norm(self, label):\n",
    "        label_vec = []\n",
    "        label_value = label \n",
    "        for i in range(10):\n",
    "            if i == label_value:\n",
    "                label_vec.append(1)\n",
    "            else:\n",
    "                label_vec.append(0)\n",
    "        return label_vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_training_data_set(num,onerow=False):\n",
    "    image_loader = ImageLoader('train-images.idx3-ubyte', num)\n",
    "    label_loader = LabelLoader('train-labels.idx1-ubyte', num)\n",
    "    return image_loader.load(onerow), label_loader.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def get_test_data_set(num,onerow=False):\n",
    "    image_loader = ImageLoader('t10k-images.idx3-ubyte', num)\n",
    "    label_loader = LabelLoader('t10k-labels.idx1-ubyte', num) \n",
    "    return image_loader.load(onerow), label_loader.load()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "def printimg(onepic):\n",
    "    onepic=onepic.reshape(28,28)\n",
    "    for i in range(28):\n",
    "        for j in range(28):\n",
    "            if onepic[i,j]==0: print('  ',end='')\n",
    "            else: print('* ',end='')\n",
    "        print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train-images.idx3-ubyte\n",
      "train-labels.idx1-ubyte\n",
      "                                                        \n",
      "                                                        \n",
      "                                                        \n",
      "                                                        \n",
      "                                                        \n",
      "      * * * * * * * * * * * * *                         \n",
      "      * * * * * * * * * * * * * * * *                   \n",
      "      * * * * * * * * * * * * * * * *                   \n",
      "      * * * * *             * * * * *                   \n",
      "                            * * * * *                   \n",
      "                        * * * * * *                     \n",
      "                      * * * * * * *                     \n",
      "                    * * * * * * * *                     \n",
      "                    * * * * * * * * * *                 \n",
      "                    * * * * * * * * * * * *             \n",
      "                        * * * * * * * * * * *           \n",
      "                                * * * * * * *           \n",
      "                                  * * * * * *           \n",
      "                                    * * * * *           \n",
      "                * * *             * * * * * *           \n",
      "              * * * *           * * * * * *             \n",
      "              * * * * *     * * * * * * * *             \n",
      "              * * * * * * * * * * * * * *               \n",
      "              * * * * * * * * * * * * *                 \n",
      "                * * * * * * * * * *                     \n",
      "                                                        \n",
      "                                                        \n",
      "                                                        \n",
      "3\n"
     ]
    }
   ],
   "source": [
    "if __name__==\"__main__\":\n",
    "    train_data_set, train_labels = get_training_data_set(100)  \n",
    "    train_data_set = np.array(train_data_set)    \n",
    "    train_labels = np.array(train_labels)\n",
    "    onepic = train_data_set[12] \n",
    "    printimg(onepic)  \n",
    "    print(train_labels[12].argmax())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "# rule\n",
    "class ReluActivator(object):\n",
    "    def forward(self, weighted_input):  \n",
    "        return max(0, weighted_input)\n",
    "\n",
    "    def backward(self, output): \n",
    "        return 1 if output > 0 else 0\n",
    "\n",
    "# IdentityActivator\n",
    "class IdentityActivator(object):\n",
    "    def forward(self, weighted_input): \n",
    "        return weighted_input\n",
    "\n",
    "    def backward(self, output): \n",
    "        return 1\n",
    "\n",
    "#Sigmoid\n",
    "class SigmoidActivator(object):\n",
    "    def forward(self, weighted_input):\n",
    "        return 1.0 / (1.0 + np.exp(-weighted_input))\n",
    "\n",
    "    def backward(self, output):\n",
    "        return np.multiply(output, (1 - output)) \n",
    "\n",
    "# tanh\n",
    "class TanhActivator(object):\n",
    "    def forward(self, weighted_input):\n",
    "        return 2.0 / (1.0 + np.exp(-2 * weighted_input)) - 1.0\n",
    "\n",
    "    def backward(self, output):\n",
    "        return 1 - output * output\n",
    "\n",
    "# softmax\n",
    "class SoftmaxActivator(object):\n",
    "     def forward(self, weighted_input):  \n",
    "         return max(0, weighted_input)\n",
    "    \n",
    "     def backward(self, output):\n",
    "         return 1 if output > 0 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
